** Preliminary for analysis

/* input 
${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta 
*/

/* output
/* Used in main analysis */
${output_data}importer_rank.dta
${output_data}exporter_rank.dta
${output_data}quota_mex_weight_hs6.dta

/* Used in bin analysis shown in Table A.18 */
${output_data}importer_rank5.dta
${output_data}importer_rank10.dta
${output_data}importer_rank20.dta
${output_data}exporter_rank5.dta
${output_data}exporter_rank10.dta
${output_data}exporter_rank20.dta
*/

capture ssc install egenmore

* Make dataset for importers' OwnRank 
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2  value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gen exporter_id=rfc
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
gsort hs6_importer_id
by hs6_importer_id: gen n_partner_2004=_N
gsort hs6_importer_id -value_exporter_importer
by hs6_importer_id: gen exporter_rank=_n
keep if exporter_rank==1
drop exporter_rank
rename exporter_id main_exporter
gsort hs6  -value newclid2
by hs6 : gen importer_own_rank=_n
by hs6 : gen n_importer_2004=_N
keep hs6_importer_id importer_own_rank n_importer_2004 n_partner_2004
save "${output_data}importer_rank.dta", replace

* Make dataset for exporters' OwnRank 
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2  value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gen exporter_id=rfc
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
gsort hs6_exporter_id
by hs6_exporter_id: gen n_partner_2004=_N
gsort hs6_exporter_id -value_exporter_importer
by hs6_exporter_id: gen importer_rank=_n
keep if importer_rank==1
drop importer_rank
rename importer_id main_importer
gsort hs6  -value rfc
by hs6 : gen exporter_own_rank=_n
by hs6 : gen n_exporter_2004=_N
keep hs6_exporter_id exporter_own_rank n_exporter_2004 n_partner_2004
save "${output_data}exporter_rank.dta", replace



** For Bin analysis shown in Table A18
foreach x in 5 10 20{
* Make dataset for importers' OwnRank 
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2 value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gen exporter_id=rfc
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
gsort hs6_importer_id -value_exporter_importer
by hs6_importer_id: gen exporter_rank=_n
keep if exporter_rank==1
drop exporter_rank
rename exporter_id main_exporter
sort hs6
by hs6 : gen n_importer_2004=_N
egen importer_own_rank=xtile(value_exporter_importer), by(hs6) n(`x')
sort hs6 importer_own_rank
by hs6 importer_own_rank:gen dup_num=_N
sort hs6 importer_own_rank dup_num
by hs6 importer_own_rank dup_num: gen index=_n
gsort hs6 -importer_own_rank index
by hs6 : gen rank_new=_n
replace rank_new=rank_new-index+1 if dup_num>1 & index!=1
drop importer_own_rank
rename rank_new importer_own_rank
keep hs6_importer_id importer_own_rank n_importer_2004
save "${output_data}importer_rank`x'.dta", replace

* Make dataset for exporters' OwnRank 
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep rfc hs6 year newclid2  value_exporter_importer
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
gen exporter_id=rfc
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gen hs6_exporter_id=hs6+"_"+ rfc
gsort hs6_exporter_id -value_exporter_importer
by hs6_exporter_id: gen importer_rank=_n
keep if importer_rank==1
drop importer_rank
rename importer_id main_importer
sort hs6
by hs6 : gen n_exporter_2004=_N
egen exporter_own_rank=xtile(value_exporter_importer), by(hs6) n(`x')
sort hs6 exporter_own_rank
by hs6 exporter_own_rank:gen dup_num=_N
sort hs6 exporter_own_rank dup_num
by hs6 exporter_own_rank dup_num: gen index=_n
gsort hs6 -exporter_own_rank index
by hs6 : gen rank_new=_n
replace rank_new=rank_new-index+1 if dup_num>1 & index!=1
drop exporter_own_rank
rename rank_new exporter_own_rank
keep hs6_exporter_id exporter_own_rank n_exporter_2004
save "${output_data}exporter_rank`x'.dta", replace
}
